package com.xzm.project.ui.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.orhanobut.logger.Logger;
import com.xzm.project.R;

import java.util.HashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import cn.wuyu.baselibrary.utils.ResUtils;
import cn.wuyu.baselibrary.utils.SizeUtils;
import cn.wuyu.baselibrary.utils.ToastUtil;

/**
 *
 */
public class TestActivity extends BaseActivity {

    @Override
    protected int getLayoutID() {
        return R.layout.activity_test1;
    }

    private String text = "哈哈哈哈哈哈哈哈" +
            "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈嗯结尾哈哈想";

    @Override
    protected void initView(Bundle savedInstanceState) {

        SparseArray<String> stringSparseArray = new SparseArray<>();
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringSparseArray.put(1,"2222");
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1,
                2,
                3L,
                TimeUnit.MINUTES, new LinkedBlockingQueue<>());
        threadPoolExecutor.execute(() -> {

        });
        final TextView tvContent = findViewById(R.id.tvContent);
        tvContent.setText(text);
        int lineCount = tvContent.getLineCount();
        Logger.d("====lineCount==" + lineCount);
        if (lineCount>1){
            tvContent.setText("");
            setEllipsize(tvContent,text,"全部",ResUtils.getColor(R.color.Red),v -> {
                ToastUtil.show("点击");
            });
        }

//        tv.setText(text);
//        tvContent.post(new Runnable() {
//            @Override
//            public void run() {
//                // 计算文字超出部分做精确截取
//                String text = guessLikeBean.getTitle() + "(精)";
//                tvContent.setText(text);
//                int ellipsisCount = tvContent.getLayout().getEllipsisCount(tvContent.getLineCount() - 1);
//                if (ellipsisCount > 0) {
//                    text = text.substring(0, text.length() - ellipsisCount - 1) + "…(精)";
//                }
//
//                // 创建SpannableString对象
//                SpannableString imageString = new SpannableString(text);
//get  getIntace
//                // 获取图片资源并设置绘制边界
////                Drawable imagehhgnwdawd = mContext.getResources().getDrawable(R.drawable.ic_recommend_job_jing2);
////                int left = SizeUtils.dp2px(mContext, 0);
////                int top = SizeUtils.dp2px(mContext, 0);
////                int right = SizeUtils.dp2px(mContext, 17);
////                int bottom = SizeUtils.dp2px(mContext, 17);
////                image.setBounds(left, top, right, bottom);
//
//                // 创建图片样式对象替换占位符
////                AlignTopImageSpan imageSpan = new AlignTopImageSpan(image, ALIGN_TOP);
////                imageString.setSpan(imageSpan, imageString.length()-3, imageString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//                mTvTitle.setText(imageString);
//            }
//        });


//        setEllipsize(tv,text,"全部", ResUtils.getColor(R.color.Red),v -> {
//            ToastUtil.show("点击更多");
//        });
//        //tv.setMovementMethod(ScrollingMovementMethod.getInstance());
//        tv.setOnClickListener(new View.OnClickListener() {
//            Boolean flag = true;
//            @Override
//            public void onClick(View v) {
//                Log.i("tv.getLineCount()",tv.getHeight()+"");
//                if(flag){
//                    flag = false;
//                    tv.setEllipsize(null); // 展开
//                    //tv.setSingleLine(flag);
//                }else{
//                    flag = true;
//                    tv.setEllipsize(TextUtils.TruncateAt.END); // 收缩
//                    //tv.setSingleLine(flag);
//                }
//            }
//        });
    }


    @Override
    protected void initListener() {

    }


    /**
     * 给TextView的超长文本省略号之后加上点击文本
     *
     * @param tv           TextView
     * @param originalText 原始的超长文本
     * @param endStr       要添加在末尾的文本
     * @param endStrColor  末尾文本颜色
     * @param listener     点击事件
     */
    public static void setEllipsize(@NonNull final TextView tv, @Nullable final String originalText,
                                    @Nullable final String endStr, @ColorInt final int endStrColor,
                                    @Nullable final View.OnClickListener listener) {
        tv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                TextPaint paint = tv.getPaint();
                // TextView最大支持文本宽度 - 要添加在末尾的文本宽度
                float avail = (tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight()) * tv.getMaxLines()
                        - paint.getTextSize() * (endStr == null ? 0 : endStr.length() + 1);
                // 获取带省略号的文本
                String string = TextUtils.ellipsize(TextUtils.isEmpty(originalText) ?
                        tv.getText() : originalText, paint, avail, TextUtils.TruncateAt.END).toString();
                // 带省略号的文本 + 添加在末尾的文本
                string = string + endStr;
                SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(string);
                // 点击事件和添加在末尾的文本效果
                spannableStringBuilder.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View view) {
                        if (listener != null) listener.onClick(view);
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        // 添加在末尾的文本颜色
                        ds.setColor(endStrColor);
                        // 添加在末尾的文本去掉下划线
                        ds.setUnderlineText(false);
                        // 清除阴影
                        ds.clearShadowLayer();
                    }
                }, string.length() - endStr.length(), string.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                // 重新设置TextView文本
                tv.setText(spannableStringBuilder);
                // 去除默认点击后背景变色效果
                tv.setHighlightColor(Color.TRANSPARENT);
                // 让点击事件可以生效
                tv.setMovementMethod(LinkMovementMethod.getInstance());
                // 取消关注，让点击事件与ListView的Item点击事件不再冲突
                tv.setFocusable(false);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        });
    }

}
